package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.exception.GlobalExceptionHelper;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.GroupDAO;
import com.jinwei.qunguanjia.user.dto.*;
import com.jinwei.qunguanjia.user.po.GroupPO;
import com.jinwei.qunguanjia.user.po.SensitiveWordPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service("groupService")
public class GroupServiceImpl implements GroupService {

    @Resource
    private GroupDAO groupDAO;

    @Resource
    private UserIntegralService userIntegralService;

    @Resource
    private UserService userService;

    @Resource
    private UserMessageService userMessageService;

    @Override
    public String save(GroupDTO groupDTO) {
        GroupPO groupPO = new GroupPO();
        BeanUtils.copyProperties(groupDTO, groupPO);
        String groupId = IdUtils.getWorkerId();
        groupPO.setGroupId(groupId);
        groupPO.setCreateTime(DateUtils.getDateTime());
        groupDAO.insert(groupPO);
        return groupId;
    }

    @Override
    @Transactional
    public void remove(String id) {
        GroupDTO groupDTO = get(id);
        UserIntegralDTO integralDTO = UserIntegralDTO.builder()
                .integralType("1")
                .integralSource("6")
                .integralNumber("20")
                .userId(groupDTO.getUserId())
                .build();
        userIntegralService.save(integralDTO);
        UserDTO userQueryDTO = userService.get(groupDTO.getUserId());
        int myIntegral = Integer.parseInt(userQueryDTO.getMyIntegral());
        if (myIntegral <= 20) {
            myIntegral = 0 ;
        } else {
            myIntegral = myIntegral - 20;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(groupDTO.getUserId());
        userDTO.setMyIntegral(String.valueOf(myIntegral));
        userService.update(userDTO);
        groupDAO.deleteById(id);
    }

    @Override
    public void update(GroupDTO groupDTO) {
        GroupPO groupPO = new GroupPO();
        BeanUtils.copyProperties(groupDTO, groupPO);
        groupPO.setUpdateTime(DateUtils.getDateTime());
        groupDAO.updateById(groupPO);
    }

    @Override
    public GroupDTO get(String id) {
        return groupDAO.get(id);
    }

    @Override
    public GroupDTO get(GroupDTO groupDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupDTO);
        // 拼接查询条件
        QueryWrapper<GroupPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        GroupPO groupPO = groupDAO.selectOne(queryWrapper);
        if (null == groupPO) {
            return null;
        }
        BeanUtils.copyProperties(groupPO, groupDTO);
        return groupDTO;
    }

    @Override
    public Long count(GroupDTO groupDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupDTO);
        // 拼接查询条件
        QueryWrapper<GroupPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return groupDAO.selectCount(queryWrapper);
    }

    @Override
    public List<GroupDTO> list(GroupDTO groupDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupDTO);
        // 拼接查询条件
        QueryWrapper<GroupPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(groupDAO.selectList(queryWrapper), GroupDTO.class);
    }

    @Override
    public PageDataInfo<GroupDTO> listPage(GroupDTO groupDTO) {
        /*LambdaQueryWrapper<GroupPO> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (!StringUtils.isEmpty(groupDTO.getGroupName()) && !StringUtils.isEmpty(groupDTO.getGroupDesc())) {
            lambdaQueryWrapper.and(wq -> {
                wq.or(wq1 -> wq1.like(GroupPO::getGroupName, groupDTO.getGroupName()));
                wq.or(wq2 -> wq2.like(GroupPO::getGroupDesc, groupDTO.getGroupDesc()));
            });
        }
        if (!StringUtils.isEmpty(groupDTO.getGroupName())) {
            lambdaQueryWrapper.like(GroupPO::getGroupName, groupDTO.getGroupName());
        }
        if (!StringUtils.isEmpty(groupDTO.getGroupDesc())) {
            lambdaQueryWrapper.like(GroupPO::getGroupDesc, groupDTO.getGroupDesc());
        }
        if (!StringUtils.isEmpty(groupDTO.getUserId())) {
            lambdaQueryWrapper.eq(GroupPO::getUserId, groupDTO.getUserId());
        }
        if (!StringUtils.isEmpty(groupDTO.getGroupStatus())) {
            lambdaQueryWrapper.eq(GroupPO::getGroupStatus, groupDTO.getGroupStatus());
        }*/
        List<GroupDTO> list = groupDAO.list(groupDTO);
        Collections.shuffle(list);
        PageInfo<GroupDTO> pageInfo = new PageInfo<>(list);
        PageDataInfo<GroupDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(list);
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public PageDataInfo<GroupDTO> listMyPublishPage(GroupDTO groupDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupDTO);
        // 拼接查询条件
        QueryWrapper<GroupPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<GroupPO> list = groupDAO.selectList(queryWrapper);
        PageInfo<GroupPO> pageInfo = new PageInfo<>(list);
        PageDataInfo<GroupDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, GroupDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(GroupDTO groupDTO) throws IllegalAccessException {
        String groupId = StringUtils.isNull(groupDTO.getGroupId()) ? "-1" : groupDTO.getGroupId();
        GroupDTO info = get(groupDTO);
        if (StringUtils.isNotNull(info) && !info.getGroupId().equals(groupId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    @Transactional
    public void auditAgree(String groupId) {
        GroupDTO groupDTO = get(groupId);
        if (!"1".equals(groupDTO.getGroupStatus())) {
            throw GlobalExceptionHelper.localServiceException("此状态下不能在审核了");
        }
        GroupPO groupPO = new GroupPO();
        groupPO.setGroupId(groupDTO.getGroupId());
        groupPO.setGroupStatus("2");
        groupPO.setUpdateTime(DateUtils.getDateTime());
        groupDAO.updateById(groupPO);
        UserIntegralDTO integralDTO = UserIntegralDTO.builder()
                .integralType("0")
                .integralSource("1")
                .integralNumber("10")
                .userId(groupDTO.getUserId())
                .build();
        userIntegralService.save(integralDTO);
        UserDTO userQueryDTO = userService.get(groupDTO.getUserId());
        int myIntegral = Integer.parseInt(userQueryDTO.getMyIntegral());
        myIntegral = myIntegral + 10;
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(groupDTO.getUserId());
        userDTO.setMyIntegral(String.valueOf(myIntegral));
        userService.update(userDTO);
        UserMessageDTO userMessageDTO = new UserMessageDTO();
        userMessageDTO.setMessageId(IdUtils.getWorkerId());
        userMessageDTO.setUserId(groupDTO.getUserId());
        userMessageDTO.setMessageType("1");
        userMessageDTO.setTitle("群审核");
        userMessageDTO.setContent("您发布的群【"+groupDTO.getGroupName()+"】已通过审核");
        userMessageService.save(userMessageDTO);
    }

    @Override
    public void auditRefuse(String groupId,String reason) {
        GroupDTO groupDTO = get(groupId);
        if (!"1".equals(groupDTO.getGroupStatus())) {
            throw GlobalExceptionHelper.localServiceException("此状态下不能在审核了");
        }
        GroupPO groupPO = new GroupPO();
        groupPO.setGroupId(groupId);
        groupPO.setGroupStatus("3");
        groupPO.setReason(reason);
        groupPO.setUpdateTime(DateUtils.getDateTime());
        groupDAO.updateById(groupPO);
        UserMessageDTO userMessageDTO = new UserMessageDTO();
        userMessageDTO.setMessageId(IdUtils.getWorkerId());
        userMessageDTO.setUserId(groupDTO.getUserId());
        userMessageDTO.setMessageType("1");
        userMessageDTO.setTitle("群审核");
        userMessageDTO.setContent("您发布的群【"+groupDTO.getGroupName()+"】审核被拒绝，原因："+reason);
        userMessageService.save(userMessageDTO);
    }
}
